package com.sky.sky_server.service.impl;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.sky_pojo.dto.DishItemVO;
import com.sky.sky_pojo.dto.SetmealDTO;
import com.sky.sky_pojo.dto.SetmealVO;
import com.sky.sky_pojo.entity.PageResult;
import com.sky.sky_pojo.entity.Setmeal;
import com.sky.sky_pojo.entity.SetmealDish;
import com.sky.sky_pojo.entity.SetmealQueryPage;
import com.sky.sky_server.mapper.SetmealDishMapper;
import com.sky.sky_server.mapper.SetmealMapper;
import com.sky.sky_server.service.SetmealService;

@Service
public class SetmealServiceImpl implements SetmealService{
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /*
     * 根据分类id查询套餐列表
     */
    @Override
    public List<Setmeal> selectByCategoryId(Long categoryId) {
        return setmealMapper.selectByCategoryId(categoryId);
    }

    /**
     * 根据套餐id查询包含的菜品
     */
    @Override
    public List<DishItemVO> selectDishBySetmealId(Long setmealId) {
        return setmealMapper.selectDishBySetmealId(setmealId);
    }

    /**
     * 新增套餐
     */
    @Override
    @Transactional
    public int save(SetmealDTO setmealDTO) {
        // 设置售卖状态为0
        setmealDTO.setStatus(0);

        // 更新套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        int res = setmealMapper.insert(setmeal);
        if(res <= 0){
            throw new RuntimeException("新增套餐失败");
        }

        Long setmealId = setmeal.getId();
        // 更新套餐菜品关系表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for(SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishMapper.insertBatch(setmealDishes);

        return 1;
    }

    /**
     * 分页查询
     */
    @Override
    public PageResult<Setmeal> page(SetmealQueryPage setmealQueryPage) {
        PageHelper.startPage(setmealQueryPage.getPage(), setmealQueryPage.getPageSize());

        List<Setmeal> setmealList = setmealMapper.selectByCondition(setmealQueryPage);
        Page<Setmeal> page = (Page<Setmeal>) setmealList;

        List<Setmeal> res = page.getResult();
        Long total = page.getTotal();
        page.close();

        return new PageResult<Setmeal>(total, res);
    }

    /**
     * 修改套餐
     */
    @Override
    @Transactional
    public int update(SetmealDTO setmealDTO) {
        // 修改套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        int res = setmealMapper.update(setmeal);
        // 修改套餐菜品关系表
        if(res <= 0){
            throw new RuntimeException("修改套餐失败");
        }
        Long setmealId = setmeal.getId();
        // 删除套餐菜品关系表中的数据
        setmealDishMapper.deleteBySetmealId(setmealId);
        // 插入套餐菜品关系表中的数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmealId);
            }
        }
        setmealDishMapper.insertBatch(setmealDishes);
        return res;
    }
    /**
     * 修改套餐状态
     */
    @Override
    public int updateStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        return setmealMapper.update(setmeal);
    }


    /**
     * 根据套餐id查询套餐
     */
    @Override
    public SetmealVO selectById(Long id) {
        // 查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        // 查询套餐对应的菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 根据套餐id删除套餐
     */
    @Override
    @Transactional
    public int deleteSetmealByIds(List<Long> ids) {
        if(ids == null || ids.size() == 0){
            throw new RuntimeException("套餐id不能为空");
        }
        // 套餐处于出售状态，不能删除
        List<Setmeal> setmeals = setmealMapper.selectBatchIds(ids);
        for (Setmeal setmeal : setmeals) {
            if (setmeal.getStatus() == 1) {
                throw new RuntimeException("套餐正在出售，不能删除");
            }
        }
        int res = setmealMapper.delteByIds(ids);
        if(res <= 0)
            throw new RuntimeException("删除失败");
        setmealDishMapper.deleteBySetmealIds(ids);
        return res;
    }
}
